package com.ruizhong.centercontroll.controller;

import com.ruizhong.centercontroll.aspect.myAnnotation.checkLogin;
import com.ruizhong.centercontroll.entity.TLanguage;
import com.ruizhong.centercontroll.server.WebSocketServer;
import com.ruizhong.centercontroll.service.TLanguageService;
import com.ruizhong.centercontroll.util.Result;
import com.ruizhong.centercontroll.util.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.security.NoSuchAlgorithmException;
import java.util.*;

@RestController
@RequestMapping("/language/api")
@CrossOrigin
public class TLanguageController {

    @Autowired
    private TLanguageService tLanguageService;

    @PostMapping("/changeLanguage/{value}")
    public Result changeLanguage(@PathVariable(value = "value") String value){
        synchronized ("changeLanguage") {
            List<TLanguage> list = tLanguageService.list();
            TLanguage tLanguage = list.get(0);
            tLanguage.setValue(value);
            tLanguageService.updateById(tLanguage);
        }
        return new Result(ResultCode.success,null,null);
    }

    @GetMapping("/getLanguage")
    @checkLogin
    public Result getLanguage(){
        String value = tLanguageService.list().get(0).getValue();
        return new Result(ResultCode.success,null,value);
    }

    @GetMapping("/checkPassword/{pwd}")
    public Result checkPassword(@PathVariable(value = "pwd") String pwd){
        Boolean res = tLanguageService.checkPassword(pwd);
        return new Result(ResultCode.success,null,res);
    }

    @GetMapping("/getPassword")
    public Result getPassword(){
        String password = tLanguageService.getPassword();
        return new Result(ResultCode.success,null,password);
    }

    @PostMapping("/submitNewPassword/{pwd}")
    public Result submitNewPassword(@PathVariable(value = "pwd") String password){
        tLanguageService.submitNewPassword(password);
        return new Result(ResultCode.success,null,null);
    }

    @GetMapping("/getRandomId")
    public Result getRandomId(HttpServletRequest request) throws NoSuchAlgorithmException {
        String randomId = tLanguageService.getRandomId(request);
        return new Result<>(ResultCode.success, null, randomId);
    }

    @PostMapping("/changeMinimizeOnStartup/{mos}")
    @checkLogin("changeMinimizeOnStartup")
    public Result changeMinimizeOnStartup(@PathVariable(value = "mos") Boolean mos){
        tLanguageService.changeMinimizeOnStartup(mos);
        return new Result(ResultCode.success,null,null);
    }

    @GetMapping("/getSettings")
    public Result getSettings(){
        Map<String,Object> settingMap = tLanguageService.getSettings();
        return new Result(ResultCode.success,null,settingMap);
    }
}
